package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Inform;
import com.baizhi.springboot.service.InformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class InformController {
    @Autowired
    private InformService informService;

    @RequestMapping("/selectInformByPage")
    public Map selectInformByPage(Integer page,Integer limit) {
        return informService.selectInformByPage(page,limit);
    }
    @RequestMapping("/insertInform")//添加表单
    public Map insertInform(Inform inform) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            inform.setSysdate(new Date());
            informService.insertInform(inform);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteInform")
    public Map  deleteInform(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            informService.deleteInform(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneInform")
    public Map selectOneInform(Integer id, HttpSession session) {
        Inform inform= informService.selectOneInform(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("inform", inform);
        return map;
    }

    @RequestMapping("/updateInform")
    public Map updateInform(Inform inform) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            //System.out.println(label);
            informService.updateInform(inform);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
